#ifndef __H_STRINGUTIL
#define __H_STRINGUTIL

// Note:	to_str() and the overloaded << operators need to be defined right here
//			in the .h file so that we can use the inline statement for optimization.

#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <vector>

#define LOWER( c )	( ( (c) >= 'A' && (c) <= 'Z' ) ? (c)+'a'-'A' : (c) )
#define UPPER( c )	( ( (c) >= 'a' && (c) <= 'z' ) ? (c)+'A'-'a' : (c) )

// Stream conversion...
template <class type> std::string to_str( type t ) {
	std::ostringstream buffer;
	buffer << t;
	return buffer.str();
}

// Overloaded << operators for strings (defined here so we can use inline)...
inline std::string& operator << (std::string& dest, const char c ) {
  dest += c;
  return dest;
}

inline std::string& operator << (std::string& dest, const char* src ) {
  dest.append(src);
  return dest;
}

inline std::string& operator << (std::string& dest, const std::string& src ) {
  dest.append(src);
  return dest;
}

inline std::string& operator << (std::string& dest, int insertion ) {
  dest.append(to_str(insertion));
  return dest;
}

inline std::string& operator << (std::string& dest, unsigned int insertion ) {
  dest.append(to_str(insertion));
  return dest;
}

inline std::string& operator << (std::string& dest, unsigned long insertion ) {
  dest.append(to_str(insertion));
  return dest;
}

// Case conversions...
std::string lowerString( const std::string& src );
std::string upperString( const std::string& src );

// Removes whitespace from the ends of a string (modeled after php's trim() family of functions)...
std::string trim( const std::string& src );
std::string ltrim( const std::string& src );
std::string rtrim( const std::string& src );
std::string loginTrim( const std::string& src );

// Parses color values...
// These have now, apparently, been depracated.  I don't feel like fixing
// them because they never get used.  Oh well.  I'll just comment them out.
char* cprint_private( char );
char* cprint_private( char, char );

// Returns a string containing ( message.length() - space ) spaces...
std::string spaces( const unsigned& space, const std::string& message );
std::string& spaces( const unsigned& space, const std::string& message, std::string& temp );

// Returns a string containing message (or number) and the rest
// filled with spaces to fit length, in the specified color...
std::string format( const unsigned& length, const std::string& message, char = 'x' );
std::string format( const unsigned& length, const int& number, char = 'x' );
std::string& format( const unsigned& length, const std::string& message, std::string& temp, char = 'x' );
std::string& format( const unsigned& length, const int& number, std::string& temp, char = 'x' );

// Checks for the presence of str2 in str1...
bool strstr( const std::string& str1, const std::string& str2 );

// Checks that str1 matches the beginning of str2...
bool str_prefix( const std::string& str1, const std::string& str2 );



///////////////////////////////////////////  Split  //////////////////////////////////////////////
std::string quotesSplit( const std::string &, const std::string & );

// split (const source, dest, dest, ...)
// Splits the source into space-delimited words.  The last destination holds the
//   rest of the string, if any remains.
std::vector< std::string >  split( const std::string &, char );

std::string::const_iterator split( const std::string &, std::string & );
std::string::const_iterator split( const std::string &, std::string &, std::string & );
std::string::const_iterator split( const std::string &, std::string &, std::string &, std::string & );
std::string::const_iterator split( const std::string &, std::string &, std::string &, std::string &, std::string & );

std::string::const_iterator split( const std::string &, std::string &, char );
std::string::const_iterator split( const std::string &, std::string &, std::string &, char );
std::string::const_iterator split( const std::string &, std::string &, std::string &, std::string &, char );
std::string::const_iterator split( const std::string &, std::string &, std::string &, std::string &, std::string &, char );

// getword (const source, dest, delim)
// Splits the source into delimited tokens.  Used by split().
std::string::const_iterator getword ( const std::string &, std::string &, const char = ' ' );
std::string::const_iterator getword ( std::string::const_iterator, std::string::const_iterator, std::string &, const char = ' ' );



#endif // #ifndef __H_STRINGUTIL
